package yxy.game.pm2.active.command.turntable;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actturntabledate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_夺宝;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.Turntable;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActTurnTableRefreshRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Map;

@MessageType(proto = Command.ActTurnTableRefresh, classtype = ActiveMessage.ActTurnTableRefreshRequest.class)
public class CMD_Active_夺宝_Refresh extends Command<ActTurnTableRefreshRequest> {

	private static final Logger logger = Logger.create(CMD_Active_夺宝_Refresh.class);
	
	@Override
	public void execute(MessageSource messageSource, ActTurnTableRefreshRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int type = request.getType();
		ActiveMessage.ActTurnTableRefreshResponse.Builder response = ActiveMessage.ActTurnTableRefreshResponse.newBuilder();

		if(type != ActiveModule_夺宝.普通夺宝 && type != ActiveModule_夺宝.高级夺宝) {
			send(messageSource, response, Result.DataError);
			logger.info(this.getClass() + " type " + type + " 不存在");
			return;
		}
		
		PlayerBean pb = PlayerBean.get(playercode);
		ServerBean sb = ServerBean.get(pb.getServerId());
		
		ActiveModule_夺宝 am = ActiveModule_夺宝.get(playercode,false);
		Turntable tt = am.gainturntable(type);
		
		if(tt.isFreerefresh()) {
			am.refreshturntable(type);
			tt = am.gainturntable(type);
			tt.setFreerefresh(false);
			tt.setFrupdatetime(DateTime.now());
			am.set(false);
		}else {
			
			int refreshcosttype = -1;
			int refreshcostid = -1;
			int refreshcostnum = -1;
			
			Map<String, actturntabledate> ttdmap = ActDataCenter.getAllData(actturntabledate.class, playercode);
			for(actturntabledate ttd : ttdmap.values()) {
				if(ttd.getType() == type) {
					refreshcosttype = ttd.getRefreshcosttype();
					refreshcostid = ttd.getRefreshcostid();
					refreshcostnum = ttd.getRefreshcostnum();
				}
			}
			
			Currency costcur = Currency.create(refreshcosttype, refreshcostid, refreshcostnum);
			
			boolean canrefresh = new CurrencyHandle().costCheck(playercode, costcur);
			if(!canrefresh) {	
				send(messageSource, response, new Result(Result.CostFail, costcur.getType().getNumber(), costcur.getDataId()));
				return;
			}
			
			new CurrencyHandle().cost(playercode, CCReason.活动_夺宝, costcur);
			
			am.refreshturntable(type);
		}
		
		response.setTti(am.deal(sb.getAreaId(), type));
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActTurnTableRefreshResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
